home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 267_01 / a26.c < prev    next >
C/C++ Source or Header  |  1989-01-11  |  12KB  |  478 lines

  1. /*
  2.     HEADER:        CUG267;
  3.     TITLE:        2650 Cross-Assembler (Portable);
  4.     FILENAME:    A26.C;
  5.     VERSION:    0.1;
  6.     DATE:        08/27/1988;
  7.     SEE-ALSO:    A26.H;
  8.     AUTHORS:    William C. Colley III;
  9. */
  10.  
  11. /*
  12.               2650 Cross-Assembler in Portable C
  13.  
  14.         Copyright (c) 1985,1987 William C. Colley, III
  15.  
  16. Revision History:
  17.  
  18. Ver    Date        Description
  19.  
  20. 0.0    AUG 1987    Derived from version 3.4 of my portable 6800/6801
  21.             cross-assembler.  WCC3.
  22.  
  23. 0.1    AUG 1988    Fixed a bug in the command line parser that puts it
  24.             into a VERY long loop if the user types a command line
  25.             like "A26 FILE.ASM -L".  WCC3 per Alex Cameron.
  26.  
  27. This file contains the main program and line assembly routines for the
  28. assembler.  The main program parses the command line, feeds the source lines
  29. to the line assembly routine, and sends the results to the listing and object
  30. file output routines.  It also coordinates the activities of everything.  The
  31. line assembly routines uses the expression analyzer and the lexical analyzer
  32. to parse the source line and convert it into the object bytes that it
  33. represents.
  34. */
  35.  
  36. /*  Get global goodies:  */
  37.  
  38. #include "a26.h"
  39.  
  40. /*  Define global mailboxes for all modules:                */
  41.  
  42. char errcode, line[MAXLINE + 1], title[MAXLINE];
  43. int pass = 0;
  44. int eject, filesp, forwd, listhex;
  45. unsigned  address, bytes, errors, listleft, obj[MAXLINE], pagelen, pc;
  46. FILE *filestk[FILES], *source;
  47. TOKEN token;
  48.  
  49. /*  Mainline routine.  This routine parses the command line, sets up    */
  50. /*  the assembler at the beginning of each pass, feeds the source text    */
  51. /*  to the line assembler, feeds the result to the listing and hex file    */
  52. /*  drivers, and cleans everything up at the end of the run.        */
  53.  
  54. static int done, ifsp, off;
  55.  
  56. void main(argc,argv)
  57. int argc;
  58. char **argv;
  59. {
  60.     SCRATCH unsigned *o;
  61.     int newline();
  62.     void asm_line();
  63.     void lclose(), lopen(), lputs();
  64.     void hclose(), hopen(), hputc();
  65.     void error(), fatal_error(), warning();
  66.  
  67.     printf("2650 Cross-Assembler (Portable) Ver 0.1\n");
  68.     printf("Copyright (c) 1985,1987 William C. Colley, III\n\n");
  69.  
  70.     while (--argc > 0) {
  71.     if (**++argv == '-') {
  72.         switch (toupper(*++*argv)) {
  73.         case 'L':   if (!*++*argv) {
  74.                 if (!--argc) { warning(NOLST);  break; }
  75.                 else ++argv;
  76.                 }
  77.                 lopen(*argv);
  78.                 break;
  79.  
  80.         case 'O':   if (!*++*argv) {
  81.                 if (!--argc) { warning(NOHEX);  break; }
  82.                 else ++argv;
  83.                 }
  84.                 hopen(*argv);
  85.                 break;
  86.  
  87.         default:    warning(BADOPT);
  88.         }
  89.     }
  90.     else if (filestk[0]) warning(TWOASM);
  91.     else if (!(filestk[0] = fopen(*argv,"r"))) fatal_error(ASMOPEN);
  92.     }
  93.     if (!filestk[0]) fatal_error(NOASM);
  94.  
  95.     while (++pass < 3) {
  96.     fseek(source = filestk[0],0L,0);  done = off = FALSE;
  97.     errors = filesp = ifsp = pagelen = pc = 0;  title[0] = '\0';
  98.     while (!done) {
  99.         errcode = ' ';
  100.         if (newline()) {
  101.         error('*');
  102.         strcpy(line,"\tEND\n");
  103.         done = eject = TRUE;  listhex = FALSE;
  104.         bytes = 0;
  105.         }
  106.         else asm_line();
  107.         pc = pc + bytes & 0x7fff;
  108.         if (pass == 2) {
  109.         lputs();
  110.         for (o = obj; bytes--; hputc(*o++));
  111.         }
  112.     }
  113.     }
  114.  
  115.     fclose(filestk[0]);  lclose();  hclose();
  116.  
  117.     if (errors) printf("%d Error(s)\n",errors);
  118.     else printf("No Errors\n");
  119.  
  120.     exit(errors);
  121. }
  122.  
  123. /*  Line assembly routine.  This routine gets expressions and tokens    */
  124. /*  from the source file using the expression evaluator and lexical    */
  125. /*  analyzer, respectively.  It fills a buffer with the machine code    */
  126. /*  bytes and returns nothing.                        */
  127.  
  128. static char label[MAXLINE];
  129. static int ifstack[IFDEPTH] = { ON };
  130.  
  131. static OPCODE *opcod;
  132.  
  133. void asm_line()
  134. {
  135.     SCRATCH int i;
  136.     int isalph(), popc();
  137.     OPCODE *find_code(), *find_operator();
  138.     void do_label(), flush(), normal_op(), pseudo_op();
  139.     void error(), pops(), pushc(), trash();
  140.  
  141.     address = pc;  bytes = 0;  eject = forwd = listhex = FALSE;
  142.     for (i = 0; i < BIGINST; obj[i++] = NOP);
  143.  
  144.     label[0] = '\0';
  145.     if ((i = popc()) != ' ' && i != '\n') {
  146.     if (isalph(i)) {
  147.         pushc(i);  pops(label);
  148.         if (find_operator(label)) { label[0] = '\0';  error('L'); }
  149.     }
  150.     else {
  151.         error('L');
  152.         while ((i = popc()) != ' ' && i != '\n');
  153.     }
  154.     }
  155.  
  156.     trash(); opcod = NULL;
  157.     if ((i = popc()) != '\n') {
  158.     if (!isalph(i)) error('S');
  159.     else {
  160.         pushc(i);  pops(token.sval);
  161.         if (!(opcod = find_code(token.sval))) error('O');
  162.     }
  163.     if (!opcod) { listhex = TRUE;  bytes = BIGINST; }
  164.     }
  165.  
  166.     if (opcod && opcod -> attr & ISIF) { if (label[0]) error('L'); }
  167.     else if (off) { listhex = FALSE;  flush();  return; }
  168.  
  169.     if (!opcod) { do_label();  flush(); }
  170.     else {
  171.     listhex = TRUE;
  172.     if (opcod -> attr & PSEUDO) pseudo_op();
  173.     else normal_op();
  174.     while ((i = popc()) != '\n') if (i != ' ') error('T');
  175.     }
  176.     source = filestk[filesp];
  177.     return;
  178. }
  179.  
  180. static void flush()
  181. {
  182.     while (popc() != '\n');
  183. }
  184.  
  185. static void do_label()
  186. {
  187.     SCRATCH SYMBOL *l;
  188.     SYMBOL *find_symbol(), *new_symbol();
  189.     void error();
  190.  
  191.     if (label[0]) {
  192.     listhex = TRUE;
  193.     if (pass == 1) {
  194.         if (!((l = new_symbol(label)) -> attr)) {
  195.         l -> attr = FORWD + VAL;
  196.         l -> valu = pc;
  197.         }
  198.     }
  199.     else {
  200.         if (l = find_symbol(label)) {
  201.         l -> attr = VAL;
  202.         if (l -> valu != pc) error('M');
  203.         }
  204.         else error('P');
  205.     }
  206.     }
  207. }
  208.  
  209. #define    diff_page(a1,a2)    (((a1) ^ (a2)) & 0xe000)
  210.  
  211. static void normal_op()
  212. {
  213.     SCRATCH unsigned attrib, from, to;
  214.     unsigned expr();
  215.     TOKEN *lex();
  216.     void do_label(), error(), unlex();
  217.  
  218.     do_label();  bytes = (attrib = opcod -> attr) & BYTES;
  219.     if (pass < 2) return;
  220.     obj[0] = opcod -> valu;  obj[1] = obj[2] = 0;
  221.     if (diff_page(pc,pc + bytes)) error('F');
  222.  
  223.     if (attrib & COMMA)
  224.     if ((lex() -> attr & TYPE) != SEP) { error('S');  return; }
  225.  
  226.     if (attrib & (REG + CC)) {
  227.     if ((lex() -> attr & TYPE) != VAL) { error('S');  return; }
  228.     switch (token.valu) {
  229.         case 3:    if (attrib & NOT_CC3) { error('C');  break; }
  230.  
  231.         case 2:
  232.         case 1:    if ((attrib & (REG + INDEX)) == (REG + INDEX))
  233.                 attrib &= ~INDEX;
  234.             obj[0] |= token.valu;  break;
  235.  
  236.         case 0:    if (attrib & ODD_R0) obj[0] = 0x60;
  237.             if (attrib & NOT_R0) error('R');
  238.             break;
  239.  
  240.         default:    error(attrib & REG ? 'R' : 'C');  break;
  241.     }
  242.     }
  243.  
  244.     if (attrib & INDIR) {
  245.     if ((lex() -> attr & TYPE) != OPR || token.valu != '*') unlex();
  246.     else obj[1] |= 0x80;
  247.     }
  248.  
  249.     switch (attrib & V_TYPE) {
  250.     case ABS_15:    if ((to = expr()) & 0x8000) error('A');
  251.             else {
  252.                 obj[1] |= (to >> 8) & 0x7f;  obj[2] = low(to);
  253.             }
  254.             break;
  255.  
  256.     case ABS_13:    if (diff_page(pc,to = expr())) error('A');
  257.             else {
  258.                 obj[1] |= (to >> 8) & 0x1f;  obj[2] = low(to);
  259.             }
  260.             break;
  261.  
  262.     case SIGN_8:    if ((to = expr()) > 0xff && to < 0xff80) error('V');
  263.             else obj[1] = low(to);
  264.             break;
  265.  
  266.     case UNSGN_8:    if ((to = expr()) > 0xff) error('V');
  267.             else obj[1] = low(to);
  268.             break;
  269.  
  270.     case REL_7:    from = pc;  goto do_rel;
  271.  
  272.     case ZREL_7:    from = 0x1ffe;
  273. do_rel:            if (diff_page(from,to = expr()) ||
  274.                 ((to = to - (from + 2) & 0x1fff) > 0x3f &&
  275.                 to < 0x1fc0)) {
  276.                 error('A');  obj[1] |= 0x7e;
  277.             }
  278.             else obj[1] |= to & 0x7f;
  279.  
  280.     case NONE:    break;
  281.     }
  282.  
  283.     if (attrib & INDEX) {
  284.     if ((lex() -> attr & TYPE) != SEP) unlex();
  285.     else if ((lex() -> attr & TYPE) == VAL) {
  286.         obj[1] |= 0x60;
  287.         if (token.valu > 3) error('R');
  288.         else obj[0] |= token.valu;
  289.         if ((lex() -> attr & TYPE) != SEP) unlex();
  290.         else if ((lex() -> attr & TYPE) != OPR) error('S');
  291.         else switch (token.valu) {
  292.             case '+':    obj[1] &= 0xbf;  break;
  293.  
  294.             case '-':    obj[1] &= 0xdf;  break;
  295.  
  296.             default:    error('S');  break;
  297.         }
  298.     }
  299.     else error('S');
  300.     }
  301.  
  302.     if (attrib & ONLY_R3)
  303.     if ((lex() -> attr & TYPE) != SEP || (lex() -> attr & TYPE) != VAL ||
  304.         token.valu != 3) error('S');
  305. }
  306.  
  307. static void pseudo_op()
  308. {
  309.     SCRATCH char *s;
  310.     SCRATCH int c;
  311.     SCRATCH unsigned *o, u;
  312.     SCRATCH SYMBOL *l;
  313.     int popc();
  314.     unsigned expr();
  315.     SYMBOL *find_symbol(), *new_symbol();
  316.     TOKEN *lex();
  317.     void do_label(), error(), fatal_error(), hseek();
  318.